$(function () {
    FastClick.attach(document.body);
    //验证表姓名
    var names = $("#name");
    var cads = $("#cad");
    var tels = $("#tel");
    var codes = $("#code");
    regname = /^([\u4e00-\u9fa5]){2,5}$/;
    var regcad = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|(X|x))$/;
    var regtel = /^1(3|4|5|7|8)\d{9}$/;

    check(names, regname);
    check(cads, regcad);
    check(tels, regtel);

    function check(inp, regEx) {
        inp.on("blur", function () {
            if (regEx.test($(this).val())) {
                $(this).next("span").html("格式正确");
                $(this).next("span").attr("class", "correct");
            } else if (($(this).val() == '')) {
                $(this).next("span").html("内容不能为空");
                $(this).next("span").attr("class", "error")
            }
            else {
                $(this).next("span").html("格式错误");
                $(this).next("span").attr("class", "error")
            }
        })
    };


    $(".content").on("focus", '#name,#cad,#tel', function () {
        if ($(this).next('span').hasClass("error")) {
            $(this).val("").next("span").html("").removeClass("error");
        }
    });

    //判断获取短信验证；是否成功；
    var age = true;
    $("#auth").on("click", function () {
        if (!age) {
            return;
        }
        ;
        age = false;
        setTimeout(function () {
            age = true;
        }, 2000);
        if ($(this).parent(".input-row").prev().find("#tel").val() == "" ||
            regtel.test($(this).parent(".input-row").prev().find("#tel").val()) == false
        ) {
            //错误信息时尾部显示内容
            var texts = "请输入正确的手机号";
            teltime(texts);
            return;
        } else {
            //点击之后，显示发送成功
            var texts = "已发送请注意查收";
            teltime(texts);
            //倒计时60s
            var time = 60;
            var that = this;
            var codetime = setInterval(function () {
                time--;
                $(that).html(time);
                $(that).attr({"disabled": "true"});
                if (time == 0) {
                    $(that).html("获取验证码");
                    $(that).removeAttr("disabled");
                    clearInterval(codetime);
                }
            }, 1000);
            //向后台发送数据；
        }
        ;
    });


    $(".fruit").attr("checked", "true");
    //获取服务条款是否点击同意\
    var flag = true;
    $("#reg").on("click", function () {
        if (!flag) {
            return;
        }
        flag = false
        setTimeout(function () {
            flag = true;
        }, 2000)
        var whether = $(".fruit").prop("checked");
        if (!whether) {
            var that = this;
            var details = "请确认是否同意服务条款";
            setLog(that, details)
        } else {
            //如果没有添加服务条款;
            if (whether === true &&
                $(this).parents(".content-padded").prevAll(".input-row:nth-of-type(1)").find("span").text() === "格式错误" && $(this).parents(".content-padded").prevAll(".input-row:nth-of-type(2)").find("span").text() === "格式错误") {
                var that = this;
                var details = "请输入正确姓名和身份证号";
                setLog(that, details);
                return;
            }
            ;
            if (whether === true &&
                $(this).parents(".content-padded").prevAll(".input-row:nth-of-type(1)").find("span").text() === "格式正确" && $(this).parents(".content-padded").prevAll(".input-row:nth-of-type(2)").find("span").text() === "格式错误") {
                var that = this;
                var details = "请输入正确的身份证号";
                setLog(that, details);
                return;
            }
            ;
            if (whether === true &&
                $(this).parents(".content-padded").prevAll(".input-row:nth-of-type(1)").find("span").text() == "格式错误" && $(this).parents(".content-padded").prevAll(".input-row:nth-of-type(2)").find("span").text() == "格式正确") {
                var that = this;
                var details = "请输入正确的姓名";
                setLog(that, details);
                return;
            }
            ;
            if (whether === true &&
                $(this).parents(".content-padded").prevAll(".input-row:nth-of-type(1)").find("span").text() == "格式正确" &&
                $(this).parents(".content-padded").prevAll(".input-row:nth-of-type(2)").find("span").text() == "格式正确") {
                var nameval = names.val();
                var cadval = cads.val();
                var codesval = codes.val();
                //传递参数
                if (nameval == num1 && cadval == num2) {
                    $(this).next("#judge").html("登录成功").show();
                    window.location.href = "../abc.html";
                } else {
                    console.log(1);
                    console.log(this)
                    var that = this;
                    var details = "登录失败";
                    setLog(that, details);
                    return
                }
            }
            ;
        }

    });


    //服务协议与隐私相关政策a
    $(".service").on("click", function () {
        var serve = $(this).parents("span").parents(".clause").siblings(".serve")
        $(serve).show();
        $(serve).find(".tu").click(function () {
            $(this).parent($(".serves")).parent(".serve").hide()
        })
    });
    $(".policy").on("click", function () {
        var serve = $(this).parents("span").parents(".clause").siblings(".privacy")
        $(serve).show();
        $(serve).find(".biao").click(function () {
            $(this).parent($(".privacys")).parent(".privacy").hide()
        })
    });

});

//验证姓名身份证件正确错误时的，尾部显示具体错误内容的函数封装
function setLog(that, details) {
    $(that).next("#judge").html(details).show(400).delay(2000).hide(400)
};
//验证手机号码时，尾部显示具体错误内容的函数封装
function teltime(texts) {
    var num = 3;
    var settime = setInterval(function () {
        num--;
        $("#judge").html(texts).show(400);
        if (num == 0) {
            $("#judge").html("").hide(400);
            clearInterval(settime);
        }
    }, 1000);
}